package cn.stylefeng.guns.rabbitmq.consumer;

import cn.stylefeng.guns.rabbitmq.model.ResultParam;
import cn.stylefeng.guns.rabbitmq.service.IBatchCardService;
import cn.stylefeng.guns.rabbitmq.service.ICardService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.IOUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 *  * @author jwj
 *  
 */
@Component
@Slf4j
public class Receiver {

    @Autowired
    Map<String, ICardService> cardChannelService;

    @Autowired
    IBatchCardService batchCardService;

    @RabbitListener(queues = "pin_card_submit")
    public void pinCardSubmit(JSONObject jsonObject, Channel channel, Message message) {
        try {
            JSONObject pchannel = jsonObject.getJSONObject("channel");
            String channelCode = pchannel.getString("channelCode");
            ICardService cardService = cardChannelService.get(channelCode);
            if (cardService != null) {
                boolean result = cardService.sendCardSubmit(jsonObject);
                if (result) {
                    log.info("提交执行成功，开始确认消息");
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                }
            }
            log.info("pin_card_submit++++++++++:" + jsonObject.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "pin_card_notify")
    public void pinCardNotify(ResultParam param, Channel channel, Message message) {
        try {
            String serviceName = param.getServiceName();
            ICardService cardService = cardChannelService.get(serviceName);
            if (cardService != null) {
                boolean result = cardService.cardNotify(param);
                if (result) {
                    log.info("回调执行成功，开始确认消息");
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                }
            }
            log.info("pin_card_notify++++++++++:" + JSONObject.toJSONString(param));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = "batch_pin_card_submit")
    public void batchPinCardSubmit(JSONObject jsonObject, Channel channel, Message message) {
        try {
            boolean result = batchCardService.sendBatchCardSubmit(jsonObject);
            if (result) {
                log.info("回调执行成功，开始确认消息");
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }
            log.info("batch_pin_card_submit，返回：" + result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
